package com.gitee.jiaojinxin.oauth2.auth.bo;

import com.gitee.jiaojinxin.oauth2.auth.dao.IMyUserDAO;
import com.gitee.jiaojinxin.oauth2.auth.model.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class MyUserBOImpl implements UserDetailsService {

    @Autowired
    private IMyUserDAO userDAO;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        MyUser user = userDAO.findOne(
                Example.of(
                        MyUser.builder().username(username).build()
                )
        ).orElseGet(MyUser::new);
        if (null == user.getUserId()) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return user;
    }
}
